package org.dromara.oce.controller.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import com.bytedance.ads.model.FileImageAdV2ResponseData;
import com.bytedance.ads.model.FileVideoAdV2ResponseData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.oce.domain.req.UploadImageReq;
import org.dromara.oce.domain.req.UploadVideoReq;
import org.dromara.oce.service.remote.OceanEngineThirdRemoteService;
import org.dromara.oce.utils.FileUtil;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * 文件上传
 *
 * @author: hhl
 * @date：2025/5/28 17:14
 * @module 管理平台接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oce/upload")
@Slf4j
@SaIgnore
public class FileController {

    private final OceanEngineThirdRemoteService oceanEngineThirdRemoteService;

    private List<String> imageTypeList = Arrays.asList("image/jpg", "image/jpeg", "image/png", "image/bmp", "image/gif");
    private List<String> videoTypeList = Arrays.asList("video/mp4", "video/mpeg", "video/3gp", "video/avi", "video/m4v");

    /**
     * 上传图片
     *
     * @return
     */
    @PostMapping(value = "/image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<FileImageAdV2ResponseData> uploadImage(@RequestPart("file") MultipartFile multipartFile,UploadImageReq req) {
        try {
            String fileMd5 = FileUtil.getFileMD5(multipartFile);
            String fileContentType = multipartFile.getContentType();
            if (!imageTypeList.contains(fileContentType)) {
                throw new ServiceException("文件格式不支持");
            }

            File file = FileUtil.convertToFile(multipartFile);
            FileImageAdV2ResponseData fileImageAd = oceanEngineThirdRemoteService.uploadImage(req.getAccountId(), req.getAdvertiserId(), file, fileMd5);

            file.delete();
            return R.ok(fileImageAd);
        } catch (Exception e) {
            log.info("上传文件发生异常,{}", e);
            return R.fail("上传失败");
        }
    }

    /**
     * 上传视频
     *
     * @param req
     * @return
     */
    @PostMapping(value = "/video", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<FileVideoAdV2ResponseData> uploadVideo(UploadVideoReq req) {
        FileImageAdV2ResponseData respDTO = null;
        try {
            String fileMd5 = FileUtil.getFileMD5(req.getFile());
            String fileContentType = req.getFile().getContentType();
            if (!videoTypeList.contains(fileContentType)) {
                throw new ServiceException("文件格式不支持");
            }

            File file = FileUtil.convertToFile(req.getFile());
            req.setFileMd5(fileMd5);
            FileVideoAdV2ResponseData fileImageAd = oceanEngineThirdRemoteService.uploadVideo(req.getAccountId(), req,file);

            return R.ok(fileImageAd);
        } catch (Exception e) {
            log.info("上传文件发生异常,{}", e);
            return R.fail("上传失败");
        }
    }


}
